Conditions | 38 |
Paths | 4 |
Total Lines | 121 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like jquery.flot.stack.js ➔ ... ➔ stackData often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /* Flot plugin for stacking data sets rather than overlyaing them. |
||
57 | function stackData(plot, s, datapoints) { |
||
58 | if (s.stack == null || s.stack === false) |
||
59 | return; |
||
60 | |||
61 | var other = findMatchingSeries(s, plot.getData()); |
||
62 | if (!other) |
||
63 | return; |
||
64 | |||
65 | var ps = datapoints.pointsize, |
||
66 | points = datapoints.points, |
||
67 | otherps = other.datapoints.pointsize, |
||
68 | otherpoints = other.datapoints.points, |
||
69 | newpoints = [], |
||
70 | px, py, intery, qx, qy, bottom, |
||
71 | withlines = s.lines.show, |
||
72 | horizontal = s.bars.horizontal, |
||
73 | withbottom = ps > 2 && (horizontal ? datapoints.format[2].x : datapoints.format[2].y), |
||
74 | withsteps = withlines && s.lines.steps, |
||
75 | fromgap = true, |
||
76 | keyOffset = horizontal ? 1 : 0, |
||
77 | accumulateOffset = horizontal ? 0 : 1, |
||
78 | i = 0, j = 0, l, m; |
||
79 | |||
80 | while (true) { |
||
81 | if (i >= points.length) |
||
82 | break; |
||
83 | |||
84 | l = newpoints.length; |
||
85 | |||
86 | if (points[i] == null) { |
||
87 | // copy gaps |
||
88 | for (m = 0; m < ps; ++m) |
||
89 | newpoints.push(points[i + m]); |
||
90 | i += ps; |
||
91 | } |
||
92 | else if (j >= otherpoints.length) { |
||
93 | // for lines, we can't use the rest of the points |
||
94 | if (!withlines) { |
||
95 | for (m = 0; m < ps; ++m) |
||
96 | newpoints.push(points[i + m]); |
||
97 | } |
||
98 | i += ps; |
||
99 | } |
||
100 | else if (otherpoints[j] == null) { |
||
101 | // oops, got a gap |
||
102 | for (m = 0; m < ps; ++m) |
||
103 | newpoints.push(null); |
||
104 | fromgap = true; |
||
105 | j += otherps; |
||
106 | } |
||
107 | else { |
||
108 | // cases where we actually got two points |
||
109 | px = points[i + keyOffset]; |
||
110 | py = points[i + accumulateOffset]; |
||
111 | qx = otherpoints[j + keyOffset]; |
||
112 | qy = otherpoints[j + accumulateOffset]; |
||
113 | bottom = 0; |
||
114 | |||
115 | if (px == qx) { |
||
116 | for (m = 0; m < ps; ++m) |
||
117 | newpoints.push(points[i + m]); |
||
118 | |||
119 | newpoints[l + accumulateOffset] += qy; |
||
120 | bottom = qy; |
||
121 | |||
122 | i += ps; |
||
123 | j += otherps; |
||
124 | } |
||
125 | else if (px > qx) { |
||
126 | // we got past point below, might need to |
||
127 | // insert interpolated extra point |
||
128 | if (withlines && i > 0 && points[i - ps] != null) { |
||
129 | intery = py + (points[i - ps + accumulateOffset] - py) * (qx - px) / (points[i - ps + keyOffset] - px); |
||
130 | newpoints.push(qx); |
||
131 | newpoints.push(intery + qy); |
||
132 | for (m = 2; m < ps; ++m) |
||
133 | newpoints.push(points[i + m]); |
||
134 | bottom = qy; |
||
135 | } |
||
136 | |||
137 | j += otherps; |
||
138 | } |
||
139 | else { // px < qx |
||
140 | if (fromgap && withlines) { |
||
141 | // if we come from a gap, we just skip this point |
||
142 | i += ps; |
||
143 | continue; |
||
144 | } |
||
145 | |||
146 | for (m = 0; m < ps; ++m) |
||
147 | newpoints.push(points[i + m]); |
||
148 | |||
149 | // we might be able to interpolate a point below, |
||
150 | // this can give us a better y |
||
151 | if (withlines && j > 0 && otherpoints[j - otherps] != null) |
||
152 | bottom = qy + (otherpoints[j - otherps + accumulateOffset] - qy) * (px - qx) / (otherpoints[j - otherps + keyOffset] - qx); |
||
153 | |||
154 | newpoints[l + accumulateOffset] += bottom; |
||
155 | |||
156 | i += ps; |
||
157 | } |
||
158 | |||
159 | fromgap = false; |
||
160 | |||
161 | if (l != newpoints.length && withbottom) |
||
162 | newpoints[l + 2] += bottom; |
||
163 | } |
||
164 | |||
165 | // maintain the line steps invariant |
||
166 | if (withsteps && l != newpoints.length && l > 0 |
||
167 | && newpoints[l] != null |
||
168 | && newpoints[l] != newpoints[l - ps] |
||
169 | && newpoints[l + 1] != newpoints[l - ps + 1]) { |
||
170 | for (m = 0; m < ps; ++m) |
||
171 | newpoints[l + ps + m] = newpoints[l + m]; |
||
172 | newpoints[l + 1] = newpoints[l - ps + 1]; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | datapoints.points = newpoints; |
||
177 | } |
||
178 | |||
189 |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.